package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_BEAUTY_ESCORT_DART_TIME_REDUCE_FROM_CROSS, desc = "镖车加速")
public class BeautyEscortReduceDartTimeCmdFromCross extends AbstractCommand {

    int cartId;
    int goodsNum;
    int realNum;
    int crossRet;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyEscortProto.CrossBeautyEscortReduceDartTimeRespMsg reqMsg = BeautyEscortProto.CrossBeautyEscortReduceDartTimeRespMsg.parseFrom(packet.getBytes());
        cartId = reqMsg.getCartId();
        goodsNum = reqMsg.getGoodsNum();
        crossRet = reqMsg.getRet();
        realNum = reqMsg.getRealNum();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ret = crossRet;
        //还原预扣除
        player.getModule(PreReduceModule.class).restorePreReduce(eItemId.BEAUTY_ESCORT_JI_XING_LING.getValue() + "=" + goodsNum);

        if(crossRet == 0){
            //真实扣除
            player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(eItemId.BEAUTY_ESCORT_JI_XING_LING.getValue() + "=" + realNum)
                ,eLogMoneyType.BeautyEscort, eLogMoneyType.BeautyEscortDartTimeConsume);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        BeautyEscortProto.BeautyEscortReduceDartTimeRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortReduceDartTimeRespMsg.newBuilder();
        respMsg.setRet(ret);
        respMsg.setCartId(cartId);
        player.sendPacket(Protocol.U_BEAUTY_ESCORT_REDUCE_DART_TIME, respMsg);
    }
}
